//
//  LKWriteAppointMessageViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/4.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKWriteAppointMessageViewController.h"
#import "LKChooseProductDetailCell.h"
#import "LKWriteAppointBottomCell.h"
#import "LKCustomerAppointmentViewController.h"
#import "LiuXSlider.h"
@interface LKWriteAppointMessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)BOOL IsExitIndexAssetOrder;
@property(nonatomic,strong)LiuXSlider *slider;
@property(nonatomic,strong)NSMutableArray *titles;
@property(nonatomic,assign)NSInteger selectValue;
@end

@implementation LKWriteAppointMessageViewController
-(LKBaseTableview *)tableview{
    if (!_tableview) {
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT , WIDTH, HEIGHT - NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped registercellClasss:@[@"LKChooseProductDetailCell",@"LKWriteAppointBottomCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.emptyDataSetDelegate = self;
        _tableview.emptyDataSetSource = self;
    }
    return _tableview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.IsExitIndexAssetOrder = NO;
    self.tableview.estimatedRowHeight  = 200;
    [self.tableview reloadData];
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:blueDefault forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [btn addTarget:self action:@selector(saveAndAddAppointProductInformation) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.title = @"填写预约信息";
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        LKWriteAppointBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWriteAppointBottomCell"];
        [[cell.noBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIButton *btn = (UIButton *)x;
            self.IsExitIndexAssetOrder = NO;
            btn.selected = !btn.selected;
            cell.yesBtn.selected = !btn.selected;
        }];
        [[cell.yesBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIButton *btn = (UIButton *)x;
            self.IsExitIndexAssetOrder = YES;
            btn.selected = !btn.selected;
            cell.noBtn.selected = !btn.selected;
        }];
        return cell;
    }
    LKChooseProductDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKChooseProductDetailCell"];
    cell.appointmodel = self.productmodel;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00000000000001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footer = [[UIView alloc] init];
    if (section == 0) {
        return footer;
    }
    NSLog(@"%@",self.productmodel.leastMoney);
    NSLog(@"%@",self.productmodel.maxLimit);
    NSInteger value = ([self.productmodel.maxLimit integerValue] - 0) / 100 ;
    NSMutableArray *titles = [[NSMutableArray alloc] init];
    dispatch_queue_t gloabalQue = dispatch_get_global_queue(0, 0);
    dispatch_async(gloabalQue, ^{
        for (NSInteger i = [self.productmodel.leastMoney integerValue] ; i < [self.productmodel.maxLimit integerValue]; i = i + value) {
            [titles addObject:[NSString stringWithFormat:@"%ld",i]];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.titles = titles;
            self.slider = [[LiuXSlider alloc] initWithFrame:CGRectMake(15, 0, WIDTH - 30, 50) titles:titles firstAndLastTitles:@[self.productmodel.leastMoney,self.productmodel.maxLimit] defaultIndex:1 sliderImage:[UIImage imageNamed:@"日历"]];
            WS(weakSelf)
            self.slider.block = ^(int index) {
                weakSelf.selectValue = index;
            };
            [footer addSubview:self.slider];
        });
    });

    
    return footer;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section == 0 ? 12:50;
}
/**
 slider的值的改变设置

 */
-(void)chooseMoneyValue:(UISlider *)slider{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section != self.dataArray.count ? 125:125;
}
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *headerView = [[UIView alloc] init];
//    headerView.backgroundColor = cellHeaderBgColor;
//    return headerView;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 12;
//}
//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    return [[UIView alloc] init];
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 0.00000000001;
//}

/**
 确定添加预约订单
 */
-(void)saveAndAddAppointProductInformation{
//    NSLog(@"最少金额%@",_productmodel.leastMoney);
//    self.selectValue = 10000;
    double value = (double)[self.titles[_selectValue] integerValue];
    NSString *slectValeu = [NSString stringWithFormat:@"%ld",[self.titles[_selectValue] integerValue] / 10000];
    
    NSDictionary *param = @{@"CustomerId":_customermodel.customerId,@"ProductId":_productmodel.productId,@"InputReverationAmount":slectValeu,@"IsExitIndexAssetOrder":@(self.IsExitIndexAssetOrder)};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:addAppointmentUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
    
        for (UIViewController *vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[LKCustomerAppointmentViewController class]]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"appointmentProductNotice" object:nil];
                [self.navigationController popToViewController:vc animated:YES];
            }
        }
       
    } failure:^(NSString * _Nullable errorMessage) {
    
        
    }];
}
-(NSMutableArray *)titles{
    if (!_titles) {
        _titles = [[NSMutableArray alloc] init];
    }
    return _titles;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
